//
//  AY_RankHomeViewController.m
//  AiYue
//
//  Created by mac on 2023/10/20.
//

#import "AY_RankHomeViewController.h"
#import "ZJScrollPageView.h"
#import "AY_RankListPage.h"

@interface AY_RankHomeViewController ()<ZJScrollPageViewDelegate, SXLoadingDelegate>

@property (nonatomic, strong) ZJScrollPageView *scrollPageView;
@property (strong, nonatomic) UIImageView *headView;
@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (strong,nonatomic) AY_LoadingView *loadingView;
@end

@implementation AY_RankHomeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self barStyleLight];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self barStyleBlack];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadData];
    self.gk_navBarAlpha = 0;
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.headView];
}

- (void)loadData {
    [self.loadingView loadingInView:self.view];
    [AYApiRequest getRankHomeConfigSuccess:^(AY_RankModel * _Nonnull pkg) {
        [self.loadingView dismiss];
        [self.datas removeAllObjects];
        self.dataSource = [NSMutableArray arrayWithArray:pkg.data];
        for (int i =0; i < pkg.data.count; i++) {
            RankListModel *model = pkg.data[i];
            [self.datas addObject:model.name];
        }
        [self loadScrollPageView];
    } failure:^(int status, NSString * _Nonnull info) {
        [self.loadingView refresh];
    }];
}

#pragma ZJScrollPageViewDelegate 代理方法
- (NSInteger)numberOfChildViewControllers {
    return self.datas.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = [[AY_RankListPage alloc] init];
    }
    AY_RankListPage *ranListVC = (AY_RankListPage *)childVc;
    RankListModel *model = self.dataSource[index];
    ranListVC.model = model;
    return childVc;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

#pragma mark- setter getter
- (void)loadScrollPageView {
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    // 渐变
    style.gradualChangeTitleColor = YES;
    //显示滚动条
    style.showLine = YES;
//    style.showTitleImage = YES;
//    style.titleImage = YImage(@"AY_rank_title_d");
    style.showImage = YES;
    style.imagePosition = TitleImagePositionCenter;
    //
    style.scrollLineColor = COLOR(@"#FFD284");
    style.scrollTitle = NO;
    style.scrollLineHeight = YNUM(3);
    style.selectedTitleColor = COLOR(@"FFD284");
    style.normalTitleColor = COLOR(@"#909090");
    style.titleFont = YFont(14);
    style.scaleTitle = YES;
    style.showImage = YES;
    style.contentColor = [UIColor clearColor];
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, YNUM(187), SCREEN_W, SCREEN_H - YNUM(187)) segmentStyle:style titles:self.datas parentViewController:self delegate:self];
    _scrollPageView.segmentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_scrollPageView];
}

- (void)setUpTitleView:(ZJTitleView *)titleView forIndex:(NSInteger)index {
    titleView.normalImage = [UIImage imageNamed:@"AY_rank_title"];
    titleView.selectedImage = [UIImage imageNamed:@"AY_rank_title_d"];
}

- (AY_LoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[AY_LoadingView alloc] initWithFrame:CGRectMake(0, YNUM(187), SCREEN_W, SCREEN_H - YNUM(187))];
        _loadingView.backgroundColor = COLOR(@"#2a2832");
        _loadingView.delegate = self;
        _loadingView.hideLeftpop = YES;
    }
    return _loadingView;
}

- (void)refreshAction {
    [self loadData];
}

- (UIImageView *)headView {
    if (!_headView) {
        _headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, YNUM(265))];
        _headView.image = YImage(@"AY_rank_home_top");
    }
    return _headView;
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}
@end
